package com.zykj.thread.concurrent.cyclicbarrier;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author zhoulj(周利军) [1217102780@qq.com]
 * @Module ID:   <(模块)类编号，可以引用系统设计中的类编号>
 * @Comments: <对此类的描述，可以引用系统设计中的描述>
 * @JDK version used:      <JDK1.8> 24
 * @since 2019/1/15-17:24
 */
public class CyclicBarrierDemo {
    static int cyclicCount = 3 ;
    public static void main(String[] args) {

        ExecutorService executorService = Executors.newCachedThreadPool();
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        for (int i = 0; i < 3; i++) {
            executorService.execute((Runnable)()->{

                try {
                    Thread.sleep(new Random().nextInt(5000));
                    System.out.println(Thread.currentThread().getName()+"到底1目的地， 已到达人数：" + (cyclicBarrier.getNumberWaiting()));
                    cyclicBarrier.await();
                    Thread.sleep(new Random().nextInt(5000));
                    System.out.println(Thread.currentThread().getName()+"到底2目的地， 已到达人数：" + (cyclicBarrier.getNumberWaiting()));
                    cyclicBarrier.await();

                    Thread.sleep(new Random().nextInt(5000));
                    System.out.println(Thread.currentThread().getName()+"到底3目的地， 已到达人数：" + (cyclicBarrier.getNumberWaiting()));
                    cyclicBarrier.await();





                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }

            });
        }

    }
}
